package com.os.lambda.chapter3;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.Character.isDigit;
import static java.util.Arrays.asList;

public class Chapter3 {

    public static void main(String[] args) {
        List<Artist> allArtists = asList(
          new Artist("BeiJi", "Matt"),
          new Artist("London", "Linus"),
          new Artist("WuXi", "Jack")
        );

        long count = allArtists.stream().filter(artist -> {
            System.out.println(artist.getName());
            return artist.isFrom("London");
        }).count();

        System.out.println(count);

        System.out.println(StringLine.STRING_LINE);

        List<String> collected = Stream.of("a", "b", "c").collect(Collectors.toList());
        System.out.println(collected);

        System.out.println(StringLine.STRING_LINE);

        List<String> collected1 = Stream.of("a", "b", "hello")
                .map(s -> s.toUpperCase())
                .collect(Collectors.toList());
        System.out.println(collected1);

        System.out.println(StringLine.STRING_LINE);

        List<String> beginningWithNumbers = Stream.of("a", "1abb", "abc1")
                .filter(v->isDigit(v.charAt(0)))
                .collect(Collectors.toList());
        System.out.println(beginningWithNumbers);

        System.out.println(StringLine.STRING_LINE);

        List<Integer> together = Stream.of(asList(1, 2), asList(3, 4), asList(7, 8, 9))
                .flatMap(numbers->numbers.stream())
                .collect(Collectors.toList());
        System.out.println(together);

        System.out.println(StringLine.STRING_LINE);

        List<Track> tracks = asList(new Track("Bakai", 524),
                new Track("Violets for you furs", 378),
                new Track("time was", 451)
                );
        Track shorestTrack = tracks.stream().min(Comparator.comparing(track -> track.getNum()))
                .get();
        System.out.println(shorestTrack);

        System.out.println(StringLine.STRING_LINE);

        //使用reduce求和
        int countReduce = Stream.of(1, 2, 3, 1000)
                .reduce(0, (acc, element)->acc + element);
        System.out.println(countReduce);

    }
}
